# Author: HZT
# Date: 2023/9/11
from datetime import datetime

from selenium.webdriver.common.by import By

from config.config_loader import config
from page.web_sk_jk import WebSkJk
from page.web_sk_jy import WebSkJy
from page.web_sk_kp import WebSkKp
from page.web_sk_top import WebSkTop
from utils import UtilsDriver, click_paste_img, login_with_session, GetLog
from page.web_ck_dd import WebCkDd
import time
from selenium import webdriver


class TestWebSkJy:
    def setup_class(self):
        self.log = GetLog.get_log()
        self.driver = UtilsDriver.get_web_driver()
        # 实例化
        self.skJk = WebSkJk(self.driver)
        self.skTop = WebSkTop(self.driver)
        self.skJy = WebSkJy(self.driver)
        self.skKp = WebSkKp(self.driver)
        # 打开网页
        self.driver.get(config.URL)
        # 登录
        login_with_session('system2', self.driver)

    def teardown_class(self):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 页面数据核对无误，搜索功能无误
    def test01_sk_kp(self):
        # 获取今天0点的时间
        today_start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
        today_start_str = today_start.strftime("%Y-%m-%d %H:%M:%S")

        # 获取当前系统时间
        current_time = datetime.now()
        current_time_str = current_time.strftime("%Y-%m-%d %H:%M:%S")
        try:
            # self.skJk.sk_jk_click_skzx()
            # time.sleep(0.5)
            # 点击卡片管理
            self.skKp.sk_kp_click_kpgl()
            # 判断交易id是否降序排列
            elements = self.skKp.sk_kp_get_kpid()
            data_keys = []
            for element in elements:
                data_keys.append(int(element.get_attribute("data-key")))
            sorted_data_keys = sorted(data_keys, reverse=True)
            assert data_keys == sorted_data_keys
            # 输入开始时间
            self.skKp.sk_kp_input_kssj(today_start_str)
            # 输入结束时间
            self.skKp.sk_kp_input_jssj(current_time_str)
            # 点击搜索
            self.skJy.sk_jy_click_ss()
            time.sleep(1)
            elements = self.driver.find_elements(By.XPATH, "//*[contains(text(),'Exception')]")
            if len(elements) > 0:
                assert False
            else:
                assert True
        except Exception as e:
            self.log.error("搜索功能出错，原因是：{}".format(e))
            self.skKp.base_get_img()
            raise

    # 接卡：点击查看卡密，上传余额图之前，卡片管理页数据正确
    def test02_sk_kp(self):
        # 点击接卡
        self.skJk.sk_jk_click_jk()
        # 定位开关元素
        switch = self.skJk.sk_jk_get_ksjk()
        # 判断开关状态
        if switch.get_attribute("aria-hidden") == "true":
            # 开关是关闭状态，点击开关
            self.skJk.sk_jk_click_ksjk()
        # 出卡
        driver_new = webdriver.Chrome()
        driver_new.maximize_window()
        driver_new.get(config.URL)
        # 登录
        login_with_session('system1', driver_new)
        count = 1  # 发卡数
        me = '100'  # 发卡金额
        filename = "steam_card.png"  # 卡图名称
        ckdd = WebCkDd(driver_new)
        ckdd.ck_dd_click_jdd()
        ckdd.ck_dd_ck(driver_new, me, filename, count)
        driver_new.close()
        try:
            self.skJk.sk_jk_get_ckkms()[0].click()
            jkid = self.skJk.sk_jk_get_ids()[0].text
            # 卡密
            jkkm = self.skJk.sk_jk_get_dms()[0].text.split('：')[1]
            # 点击卡片管理
            self.skKp.sk_kp_click_kpgl()
            # 获取卡片id
            kmid = self.skKp.sk_kp_get_kpid()[0].get_attribute("data-key")
            # 状态：Checking available balance
            zt = self.skKp.sk_kp_get_zt()
            # 面值
            mz = self.skKp.sk_kp_get_mz()
            # 汇率
            hl = self.skKp.sk_kp_get_hl()
            # 卡密
            km = self.skKp.sk_kp_get_km()
            assert jkid == kmid
            assert "Checking available balance" == zt
            assert mz == me
            assert hl == '8.8000'
            assert jkkm == km
        except Exception as e:
            self.log.error("出错，原因是：{}".format(e))
            self.skKp.base_get_img()
            raise

    # 接卡：点击上传余额图之后，卡片管理页数据正确
    def test03_sk_kp(self):
        try:
            # 点击接卡
            self.skJk.sk_jk_click_jk()
            # 点击上传余额图
            time.sleep(1)
            self.skJk.sk_jk_click_scye()
            element = self.skJk.sk_jk_get_yetsc()
            click_paste_img("test001.png", element, self.driver)
            time.sleep(2)
            self.skJk.sk_jk_click_yettj()
            time.sleep(1)
            jkid = self.skJk.sk_jk_get_ids()[0].text
            me = self.skJk.sk_jk_get_mes()[0].text
            # 卡密
            jkkm = self.skJk.sk_jk_get_dms()[0].text.split('：')[1]
            # 点击卡片管理
            self.skKp.sk_kp_click_kpgl()
            # 获取卡片id
            kmid = self.skKp.sk_kp_get_kpid()[0].get_attribute("data-key")
            # 状态：Checking available balance
            zt = self.skKp.sk_kp_get_zt()
            # 面值
            mz = self.skKp.sk_kp_get_mz()
            # 汇率
            hl = self.skKp.sk_kp_get_hl()
            # 卡密
            km = self.skKp.sk_kp_get_km()
            assert jkid == kmid
            assert "Processing" == zt
            assert mz == str(int(float(me)))
            assert hl == '8.8000'
            assert jkkm == km
        except Exception as e:
            self.log.error("出错，原因是：{}".format(e))
            self.skKp.base_get_img()
            raise

    # 接卡：点击修改卡片，卡片管理页数据正确
    def test04_sk_kp(self):
        try:
            # 点击接卡
            self.skJk.sk_jk_click_jk()
            # 修改卡种
            self.skJk.sk_jk_click_xgkz()
            time.sleep(1)
            self.skJk.sk_jk_input_mz("50")
            time.sleep(1)
            click_paste_img("steam_card.png", self.skJk.sk_jk_get_xgtp(), self.driver)
            time.sleep(2)
            self.skJk.sk_jk_click_xgtj()
            time.sleep(1)
            jkid = self.skJk.sk_jk_get_ids()[0].text
            me = self.skJk.sk_jk_get_mes()[0].text
            # 卡密
            jkkm = self.skJk.sk_jk_get_dms()[0].text.split('：')[1]
            # 点击卡片管理
            self.skKp.sk_kp_click_kpgl()
            # 获取卡片id
            kmid = self.skKp.sk_kp_get_kpid()[0].get_attribute("data-key")
            # 状态：Checking available balance
            zt = self.skKp.sk_kp_get_zt()
            # 面值
            mz = self.skKp.sk_kp_get_mz()
            # 汇率
            hl = self.skKp.sk_kp_get_hl()
            # 卡密
            km = self.skKp.sk_kp_get_km()
            assert jkid == kmid
            assert "Processing" == zt
            assert mz == str(int(float(me)))
            assert mz == '50'
            assert hl == '8.8000'
            assert jkkm == km
        except Exception as e:
            self.log.error("出错，原因是：{}".format(e))
            self.skKp.base_get_img()
            raise

    # 接卡：点击处理成功之后，卡片管理页数据正确
    def test05_sk_kp(self):
        try:
            # 点击接卡
            self.skJk.sk_jk_click_jk()
            jkid = self.skJk.sk_jk_get_ids()[0].text
            me = self.skJk.sk_jk_get_mes()[0].text
            # 卡密
            jkkm = self.skJk.sk_jk_get_dms()[0].text.split('：')[1]
            self.skJk.sk_jk_click_clcg()
            time.sleep(1)
            self.skJk.sk_jk_click_cgtj()

            # 点击卡片管理
            self.skKp.sk_kp_click_kpgl()
            # 获取卡片id
            kmid = self.skKp.sk_kp_get_kpid()[0].get_attribute("data-key")
            # 状态：Checking available balance
            zt = self.skKp.sk_kp_get_zt()
            # 面值
            mz = self.skKp.sk_kp_get_mz()
            # 汇率
            hl = self.skKp.sk_kp_get_hl()
            # 卡密
            km = self.skKp.sk_kp_get_km()
            assert jkid == kmid
            assert "Success" == zt
            assert mz == str(int(float(me)))
            assert hl == '8.8000'
            assert jkkm == km
        except Exception as e:
            self.log.error("出错，原因是：{}".format(e))
            self.skKp.base_get_img()
            raise

    # 处理失败的卡，页面数据显示正确
    def test06_sk_kp(self):
        # 点击接卡
        self.skJk.sk_jk_click_jk()
        # 定位开关元素
        switch = self.skJk.sk_jk_get_ksjk()
        # 判断开关状态
        if switch.get_attribute("aria-hidden") == "true":
            # 开关是关闭状态，点击开关
            self.skJk.sk_jk_click_ksjk()
        # 出卡
        driver_new = webdriver.Chrome()
        driver_new.maximize_window()
        driver_new.get(config.URL)
        # 登录
        login_with_session('system1', driver_new)
        count = 1  # 发卡数
        me = '100'  # 发卡金额
        filename = "steam_card.png"  # 卡图名称
        ckdd = WebCkDd(driver_new)
        ckdd.ck_dd_click_jdd()
        ckdd.ck_dd_ck(driver_new, me, filename, count)
        driver_new.close()
        try:
            self.skJk.sk_jk_scyet(self.driver)
            jkid = self.skJk.sk_jk_get_ids()[0].text
            # 卡密
            jkkm = self.skJk.sk_jk_get_dms()[0].text.split('：')[1]
            # 处理失败
            self.skJk.sk_jk_click_clsb()
            time.sleep(1)
            self.skJk.sk_jk_click_sbyy()
            time.sleep(1)
            self.skJk.sk_jk_click_xxyw()
            time.sleep(1)
            click_paste_img("test001.png", self.skJk.sk_jk_get_sbtp(), self.driver)
            time.sleep(1)
            # 点击失败提交
            self.skJk.sk_jk_click_sbtj()
            time.sleep(3)
            self.skJk.sk_jk_click_tcok()
            # 点击卡片管理
            self.skKp.sk_kp_click_kpgl()
            # 获取卡片id
            kmid = self.skKp.sk_kp_get_kpid()[0].get_attribute("data-key")
            # 状态：Checking available balance
            zt = self.skKp.sk_kp_get_zt()
            # 面值
            mz = self.skKp.sk_kp_get_mz()
            # 汇率
            hl = self.skKp.sk_kp_get_hl()
            # 卡密
            km = self.skKp.sk_kp_get_km()
            # 说明
            sm = self.skKp.sk_kp_get_sm()
            assert jkid == kmid
            assert "Failed" == zt
            assert mz == me
            assert hl == '8.8000'
            assert jkkm == km
            assert '失败原因：卡片信息有误' in sm
        except Exception as e:
            self.log.error("搜索功能出错，原因是：{}".format(e))
            self.skKp.base_get_img()
            raise

    # 处理失败的卡，可以手动成功
    def test07_sk_kp(self):
        try:
            # 点击操作
            self.skKp.sk_kp_click_cz()
            time.sleep(1)
            # 点击手动成功
            self.skKp.sk_kp_click_sdcg()
            time.sleep(1)
            # 点击提交
            self.skKp.sk_kp_click_tj()
            time.sleep(3)
            # 获取弹窗提示
            text = self.skKp.sk_kp_get_tcts()
            assert '操作完成' == text
            self.skKp.sk_kp_click_ok()
        except Exception as e:
            self.log.error("出错，原因是：{}".format(e))
            self.skKp.base_get_img()
            raise
